using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using AutumnEngine.Modules;

namespace AutumnEngine
{
    public static partial class Engine
    {
        /// <summary>
        /// Holds and manages the various modules within the engine.
        /// </summary>
        public static partial class Modules
        {
            internal static Dictionary<string, Module> mods;

            public static Module GetModule(string name)
            {
                return mods[name];
            }
            public static void AddModule(string name, Module module)
            {
                if (Modules.Contains("Engine.Logging"))
                    Modules.Logging.Write("Adding Module \"" + name + "\"", LogLevel.Debug);
                module.Create();
                mods.Add(name, module);
            }

            public static bool Contains(string module)
            {
                return mods.ContainsKey(module);
            }

            internal static void Update()
            {
                foreach (KeyValuePair<string, Module> pair in mods)
                {
                    if (pair.Value.IsDisabled == false)
                        pair.Value.Update();
                }
            }
            internal static void Draw()
            {
                foreach (KeyValuePair<string, Module> pair in mods)
                {
                    if (pair.Value.IsDisabled == false)
                    {
                        //Short circuit, could be done better.
                        if (pair.Key != "Engine.Debug")
                            pair.Value.Draw();
                    }
                }

                //This guarantees that debug info will be drawn ontop of everything else.
                if (Modules.Contains("Engine.Debug"))
                    Modules.GetModule("Engine.Debug").Draw();
            }

            public static void RemoveModule(string name)
            {
                if (mods[name].Removable)
                {
                    mods[name].Removed();
                    mods.Remove(name);
                    if (Modules.Contains("Engine.Logging"))
                        Modules.Logging.Write("Removed Module \"" + name + "\"", LogLevel.Debug);
                }
            }

            public static void DisableModule(string name)
            {
                if (mods[name].CanBeDisabled)
                {
                    mods[name].IsDisabled = true;
                    if (Modules.Contains("Engine.Logging"))
                        Modules.Logging.Write("Disabled Module \"" + name + "\"", LogLevel.Debug);
                }
            }

            public static void EnableModule(string name)
            {
                if (mods.ContainsKey(name))
                    if (mods[name].CanBeDisabled)
                    {
                        mods[name].IsDisabled = false;
                        mods[name].Enabled();
                        if (Modules.Contains("Engine.Logging"))
                            Modules.Logging.Write("Enabled Module \"" + name + "\"", LogLevel.Debug);
                    }
            }
        }
    }
}
